const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const formatNum = e =>{  //正则验证金额输入框格式
  console.log(e)
  e.detail.value = e.detail.value.replace(/^(\-)*(\d+)\.(\d{6}).*$/, '$1$2.$3')
  e.detail.value = e.detail.value.replace(/[\u4e00-\u9fa5]+/g, ""); //清除汉字
  e.detail.value = e.detail.value.replace(/[^\d.]/g, ""); //清楚非数字和小数点
  e.detail.value = e.detail.value.replace(/^\./g, ""); //验证第一个字符是数字而不是 
  e.detail.value = e.detail.value.replace(".", "").replace(/\./g, "")//清楚小数点 
}
  // 处理未来七天的函数
  const dealTime = (num) => {     // num：未来天数
    var time = new Date()     // 获取当前时间日期
    var date = new Date(time.setDate(time.getDate() + num)).getDate()  //这里先获取日期，在按需求设置日期，最后获取需要的
    var month = time.getMonth() + 1   // 获取月份
    var day = time.getDay()   //  获取星期
    switch (day) {             //  格式化
      case 0: day = "周日"
        break
      case 1: day = "周一"
        break
      case 2: day = "周二"
        break
      case 3: day = "周三"
        break
      case 4: day = "周四"
        break
      case 5: day = "周五"
        break
      case 6: day = "周六"
        break
    }
    var obj = {
      date: date,
      day: day,
      month: month,
      newday:month + '-' + date
    }
    return obj      // 返回对象
  }

// 随机生成uid 
const wxuuid = function () {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";
  var uuid = s.join("");
  return uuid 
}

module.exports = {
  formatTime: formatTime,
  formatNum:formatNum,
  dealTime:dealTime,
  wxuuid: wxuuid
}

